<?php
	header('Content-Type: application/json');
	require_once("user.php");
	require_once("company.php");
	require_once("unit.php");
	require_once("managefiledocument.php");
	require_once("manageunit.php");
	
	
	$manageUnit=new ManageUnit();
	
	
	$key ="user";
	$user=null;
	if(session_id() == '') {
		session_start();
	}
	$key = "user";
	$user = unserialize ( $_SESSION [$key] );
	$unitId=$user->getUnitId();
	$canViewAllDocuments=$user->isActionAllowed("CAN_VIEW_ALL_DOCUMENTS","GLOBAL");
	$units=array();
	if($canViewAllDocuments){
		$units=$manageUnit->getUnitList(null,true);
	}else{
		$units=$manageUnit->getUnitList($unitId,false);
	}
?>

<?php 

$manageFileDocument=new ManageFileDocuments();

if($_SERVER['REQUEST_METHOD'] == "GET"){
	
	$responseJson='';
	$documents=null;
	
	//Get the fulldetils of documents(for display in table)
	if (array_key_exists("fulldetails",$_REQUEST)){
 		$documents=$manageFileDocument->listDocuments();
		
		$responseJson .="[";
		
		if(count($documents)==0){
			$user=$users[0];
			$responseJson .="{";
			$responseJson .= '"id":"' .$documents->getId() .'",';
			$responseJson .= '"userName":"' .$documents->getUserName() .'",';
			$responseJson .= '"unitName":"' .$documents->getUnit() .'",';
			$responseJson .= '"documentName":"' .$documents->getDocumentName() .'",';
			$responseJson .= '"size":"' .$documents->getSize() .'",';
			$responseJson .= '"uploadedOn":"' .$documents->getUploadedOn() .'",';
			$responseJson .= '"url":"' .$documents->getUrl() .'"';
			$responseJson .="}";
				
		}else {
			foreach($documents as $document){
				$responseJson .="{";
				$responseJson .= '"id":"' .$document->getId() .'",';
				$responseJson .= '"userName":"' .$document->getUserName() .'",';
				$responseJson .= '"unitName":"' .$document->getUnit() .'",';
				$responseJson .= '"documentName":"' .$document->getDocumentName() .'",';
				$responseJson .= '"size":"' .$document->getSize() .'",';
				$responseJson .= '"uploadedOn":"' .$document->getUploadedOn() .'",';
				$responseJson .= '"url":"' .$document->getUrl() .'"';
				$responseJson .="},";
			}
			$responseJson=substr($responseJson,0,strlen($responseJson)-1);
		}
		$responseJson .="]"; 
 	}else {//for update & delete operation, get the information about the selected role
  		if (array_key_exists("key",$_REQUEST)){

		}else {//For create operation 
			$responseJson='[{"id":"datafile","label":"Select Document","display":"true","type":"input_file"},';
			$unitStrs=array();
			foreach($units as $unit) {
				$unitStr='{"op_value":"' .$unit->getId() .'","op_label": "' .$unit->getUnitShort() .'"}';
				array_push($unitStrs,$unitStr);
			}
			$responseJson .='{ "id":"unit_select", "type":"input_select","label":"Unit","display":"true", "value" :[' .implode(",",$unitStrs) .']}';
				
			
			$responseJson .=']';
		}
	}
	echo $responseJson;
} elseif ($_SERVER['REQUEST_METHOD'] == "POST"){
/* 	if (array_key_exists("action",$_REQUEST)){
		$action= $_REQUEST["action"];
		
		$success ='{"status" :"success"}';
		$failure ='{"status" :"faiure"}';
		$status=true;
		
		$modifiable='NO';
		$deletable='NO';
		if(array_key_exists ("modifiable", $_REQUEST)){
			$modifiable=($_REQUEST["modifiable"][0]=="on"?"YES":"NO");
		}
		if(array_key_exists ("deletable", $_REQUEST)){
			$deletable=($_REQUEST["deletable"][0]=="on"?"YES":"NO");
		}
		
		if($action=="create"){
			$status=$userManager->addUser($_REQUEST["user_name"],$_REQUEST["password"],$_REQUEST["first_name"],$_REQUEST["last_name"],$_REQUEST["email"],$deletable,$modifiable);
			if($status){echo $success;}
			else {echo $failure;}
		}elseif($action=="update"){
			$status=$userManager->updateUser($_REQUEST["key"],$_REQUEST["user_name"],$_REQUEST["password"],$_REQUEST["first_name"],$_REQUEST["last_name"],$_REQUEST["email"],$deletable,$modifiable);
			if($status){echo $success;}
			else {echo $failure;}
		}else if($action=="delete"){
			$status=$userManager->deleteUser($_REQUEST["key"]);
			if($status){echo $success;}
			else {echo $failure;}
		}else{
			echo $failure;
		}
		
	}else{
		echo $failure;
	}*/
}
 ?>